package com.example.a02back.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a02back.dto.*;
import com.example.a02back.entity.Card;
import com.example.a02back.entity.Topic;
import com.example.a02back.entity.Trade;
import com.example.a02back.entity.User;
import com.example.a02back.mapper.CardMapper;
import com.example.a02back.mapper.TopicMapper;
import com.example.a02back.mapper.UserMapper;
import com.example.a02back.service.ITopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.a02back.utils.FileUtils;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SimpChaos
 * @since 2022-07-02
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements ITopicService {


    @Resource
    private UserMapper userMapper;
    @Resource
    private CardMapper cardMapper;

    public String publish(String topicId,String  uid, String headline, String content, String date, Integer activeValue,String actionAddress,String actionPosition){
        Topic t = new Topic();
        t.setActiveValue(activeValue);
        t.setUid(uid);
        t.setTopicId(topicId);
        t.setTime(date);
        t.setContent(content);
        t.setHeadline(headline);
        t.setIsVisible(1);
        t.setActionAddress(actionAddress);
        t.setActionPosition(actionPosition);
        String result = String.valueOf(this.save(t));
        return result;
    }


/**
 * @Description: 发布活动图片
 * @Author: SimpChaos
 * @Date: 2022/7/15 10:12
 * @Param: [file, uid, topicid]
 * @Return: java.lang.String
 */
    public String publishImage(MultipartFile file, String uid, String topicid) throws IOException {
        //得到url
        String path = FileUtils.upload(file,uid,topicid);
        //if判断是否有过
        LambdaQueryWrapper<Topic> wrapper1 = new LambdaQueryWrapper<>();
        LambdaUpdateWrapper<Topic> wrapper2 = new LambdaUpdateWrapper<>();
        wrapper1.eq(Topic::getTopicId,topicid);
        List<String> l = new ArrayList<>();
        String s = this.getOne(wrapper1).getImgList();
        if(s == null){
            l.add(path);
            String jiujiuwo = JSONObject.toJSONString(l);
            wrapper2.eq(Topic::getTopicId,topicid).set(Topic::getImgList,jiujiuwo).set(Topic::getImgFirst,path);
            String rows = String.valueOf(this.update(null,wrapper2));
            System.out.print(rows);
            if(rows.equals("ture")) return jiujiuwo;
            else return "failed to publish";
        }
        else{
            List<String> LIST = JSONObject.parseArray(s,String.class);
            LIST.add(path);
            String GAOSIWO = JSONObject.toJSONString(LIST);
            wrapper2.eq(Topic::getTopicId,topicid).set(Topic::getImgList,GAOSIWO);
            String rows = String.valueOf(this.update(null,wrapper2));
            System.out.print(rows);
            if(rows.equals("ture")) return GAOSIWO;
            else return "failed to publish";
        }
        //json转
    }


    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/15 10:14
     * @Param: [sid]
     * @Return: com.example.a02back.dto.TradeDetail
     */
    public TopicDetail getOne(String topicid){
        TopicDetail w = new TopicDetail();

        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Topic::getTopicId,topicid);
        Topic t = this.getOne(wrapper);
        BeanUtil.copyProperties(t,w);
        LambdaQueryWrapper<User> wrapper1 = new LambdaQueryWrapper<User>();
        wrapper1.eq(User::getUid,t.getUid());
        LambdaQueryWrapper<Card> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Card::getUid,t.getUid());

        Card c = cardMapper.selectOne(wrapper2);
        User u = userMapper.selectOne(wrapper1);

        w.setUsrName(u.getNickname());
        w.setUsrImg(u.getAvatarUrl());
        w.setIdentity(c.getOrgnizinfo()+" "+c.getAuthinfo());
        w.setImgList(JSONObject.parseArray(t.getImgList(),String.class));
        return w;
    }


    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/15 10:46
     * @Param: [decode, activeValue]
     * @Return: java.util.List<com.example.a02back.dto.TopicListDetail>
     */
    public List<TopicListDetail> getTopicList(Long decode ,Integer activeValue) {
        Page<Topic> page = new Page<>(decode, 10);
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper();
        if (activeValue == 0) {
            return getList(page, wrapper);
        }
        wrapper.eq(Topic::getActiveValue, 2);
        return getList(page, wrapper);
    }

    public List<TopicListDetail> getList(Page<Topic> page, LambdaQueryWrapper<Topic> wrapper) {
        List<Topic> records = this.page(page, wrapper).getRecords();
        TopicListDetail w = new TopicListDetail();
        List<TopicListDetail> result = new ArrayList<>();
        for (Topic t : records) {
            BeanUtil.copyProperties(t, w);
            LambdaQueryWrapper<User> wrapper1 = new LambdaQueryWrapper<User>();
            wrapper1.eq(User::getUid, t.getUid());
            User u = userMapper.selectOne(wrapper1);
            w.setAid(t.getTopicId());
            result.add(w);
        }
        return result;
    }


/**
 * @Description: 
 * @Author: SimpChaos 
 * @Date: 2022/7/15 23:00
 * @Param: [list]
 * @Return: java.util.List<com.example.a02back.dto.TopicListDetail>
 */
    public  List<TopicListDetail> getFavTopicList(List<String> list){
        List<TopicListDetail> list1 = new ArrayList<>();
        TopicListDetail my = new TopicListDetail();
        for(String s:list){
            Topic t = this.getById(s);
            BeanUtil.copyProperties(t,my);
            my.setAid(t.getTopicId());
            list1.add(my);
        }
        return list1;
    }


    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/15 23:02
     * @Param: [uid]
     * @Return: java.util.List<com.example.a02back.dto.TopicListDetail>
     */
    public List<TopicListDetail> getMyTopic(String uid){
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Topic::getUid,uid);
        List<Topic> list = this.list(wrapper);
        List<TopicListDetail> list1 =new ArrayList<>();
        TopicListDetail detail = new TopicListDetail();
        for(Topic t:list){
            BeanUtil.copyProperties(t,detail);
            detail.setAid(t.getTopicId());
            list1.add(detail);
        }
        return list1;
    }


    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/16 1:50
     * @Param: [topicId, headline, content, date, activeValue]
     * @Return: java.lang.String
     */
    public String update(String topicId,String headline, String content, String date, Integer activeValue,String actionAddress,String actionPosition){
        Topic t = this.getById(topicId);
        t.setActiveValue(activeValue);
        t.setTime(date);
        t.setContent(content);
        t.setHeadline(headline);
        t.setIsVisible(1);
        t.setActionAddress(actionAddress);
        t.setActionPosition(actionPosition);
        return String.valueOf(this.updateById(t));
    }

    public String updateImg(MultipartFile file,Integer number,String uid,String topicId) throws IOException {
        String path = FileUtils.upload(file,uid,topicId);
        List<String> imgList = JSONObject.parseArray(this.getById(topicId).getImgList(),String.class);
        //从0开始
        imgList.set(number-1,path);
        String newList = JSONObject.toJSONString(imgList);
        LambdaUpdateWrapper<Topic> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Topic::getTopicId,topicId).set(Topic::getImgList,newList);
        return String.valueOf(this.update(wrapper));
    }




    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/16 1:55
     * @Param: [topicId]
     * @Return: java.lang.String
     */
    public String delete(String topicId){
        LambdaUpdateWrapper<Topic> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Topic::getTopicId,topicId).set(Topic::getIsVisible,0);
        return String.valueOf(this.update(wrapper));
    }
    
    
    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 3:41
     * @Param: [keyword]
     * @Return: java.util.List<com.example.a02back.entity.Topic>
     */
    public List<Topic> searchByKeyword(String keyword){
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Topic::getHeadline,keyword).or().like(Topic::getContent,keyword);
        return this.list(wrapper);
    }

}
